﻿/*
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
*/
using System.Collections.Specialized;
using System.Linq;
using System;
using System.Management;
using Microsoft.Win32;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.LookAndFeel;
using DevExpress.Skins;
using DevExpress.XtraBars;
using DevExpress.XtraBars.Ribbon;
using DevExpress.XtraEditors;
using System.IO;
using Microsoft.ConfigurationManagement.ManagementProvider;
using Microsoft.ConfigurationManagement.ManagementProvider.WqlQueryEngine;

namespace SCCMHealthCheck
{
  class DataConverter
  {
    public string ConvertFrom(string HealthCheckName, string Field, string Value)
    {
      switch (HealthCheckName.ToLower())
      {
        case "serverinformation":
          return ServerInformation(Field, Value);
        case "boundaryinformation":
        case "collectioninformation":
        case "queryinformation":
        case "siteinformation":
        case "packagesinformation":
          return SiteInformation(Field, Value);
        case "portinformation":
        case "wolinformation":
        case "computerclientagent":
        case "reminders":
          return WOLInformation(Field, Value);
        case "advertisedprogramclientagent":
        case "advertnotification":
        case "hardwareinventoryagent":
        case "dcminventoryagent":
        case "networkaccessprotectionagent":
        case "softwaremeteringagent":
          return AdvertisedInformation(Field, Value);
        default:
          return Value;
      }
    }

    private string AdvertisedInformation(string Field, string Value)
    {
      switch (Field.ToLower())
      {
        case "effective time in utc":
        case "force scan":
        case "show icon":
        case "countdown signal":
        case "audible signal on available":
        case "new program notification ui":
        case "flag":
        case "flags":
        case "request user policy":
        case "virtualappenabled":
          switch (Value)
          {
            case "0":
            case "RAP":
              return "Disabled";
            case "1":
            case "ARP":
              return "Enabled";
            default:
              return Value;
          }
        case "inventory schedule":
          return ScheduleConverter(Value);
        case "mif collection":
          switch (Value)
          {
            case "0":
              return "Disabled";
            case "4":
              return "Collect NOIDMIF files";
            case "8":
              return "Collect IDMIF files";
            case "12":
              return "Collect IDMIF and NOIDMIF files";
            default:
              return Value;
          }
        default:
          return Value;
      }
    }

    private string WOLInformation(string Field, string Value)
    {
      switch (Field.ToLower())
      {
        case "flag":
          switch (Value)
          {
            case "2":
              return "Disabled";
            case "6":
              return "Enabled";
            default:
              return Value;
          }
        case "iiswebsitename":
        case "enable bits max bandwidth":
        case "apply to all clients":
        case "enable download off schedule":
          switch (Value)
          {
            case "0":
              return "Disabled";
            case "1":
              return "Enabled";
            default:
              return Value;
          }
        case "sendretryinterval":
        case "reboot logoff notification final window":
        case "hourreminderinterval":
          return Convert.ToString(Math.Abs(Convert.ToInt32(Value) / 60));
        case "reminderinterval":
        case "dayreminderinterval":
          return Convert.ToString(Math.Abs(Convert.ToInt32(Value) / 3600));
        default:
          return Value;
      }
    }

    private string SiteInformation(string Field, string Value)
    {
      switch (Field.ToLower())
      {
        case "type":
          switch (Value)
          {
            case "2":
              return "Primary Site";
            case "1":
              return "Secondary Site";
            default:
              return Value;
          }
        case "buildnumber":
          switch (Value)
          {
            case "5931":
              return Value + " - RTM";
            case "6221":
              return Value + " - SP1";
            case "6487":
              return Value + " - SP2";
            default:
              return Value;
          }
        case "status":
          switch (Value)
          {
            case "1":
              return "Active";
            case "2":
              return "Pending";
            case "3":
              return "Failed";
            case "4":
              return "Deleted";
            case "5":
              return "Upgrade";
            default:
              return Value;
          }
        case "boundarytype":
          switch (Value)
          {
            case "0":
              return "IPv4";
            case "1":
              return "Active Directory Site";
            case "2":
              return "IPv6 Prefix";
            case "3":
              return "IPv4 Range";
            default:
              return Value;
          }
        case "currentstatus":
          switch (Value)
          {
            case "0":
              return "None";
            case "1":
              return "Ready";
            case "2":
              return "Refreshing";
            case "3":
              return "Saving";
            case "4":
              return "Evaluating";
            case "5":
              return "Awaiting Refresh";
            case "6":
              return "Deleting";
            case "7":
              return "Appending Member";
            case "8":
              return "Querying";
            default:
              return Value;
          }
        case "refreshtype":
          switch (Value)
          {
            case "1":
              return "Manual";
            case "2":
              return "Periodic";
            default:
              return Value;
          }
        case "lastchangetime":
        case "lastmemberchangetime":
        case "lastrefreshtime":
          return ManagementDateTimeConverter.ToDateTime(Value).ToLongDateString();
        case "targetclassname":
          switch (Value.ToLower())
          {
            case "sms_r_system":
              return "Systems";
            case "sms_r_user":
              return "Users";
            case "sms_r_usergroup":
              return "user Group";
            case "sms_statusmessage":
              return "Status Messages";
            default:
              return Value;
          }
        case "isr2capablertm":
          switch (Value.ToLower())
          {
            case "31":
              return "R2";
            case "63":
              return "R3";
            default:
              return Value;
          }
        case "iissslstate":
          switch (Value.ToLower())
          {
            case "0":
              return "Mixed Mode";
            case "31":
              return "native mode, no CRL checking, no HTTP fallback (for roaming)";
            case "63":
              return "native mode, CRL checking";
            case "95":
              return "ative mode, HTTP fallback";
            case "127":
              return "native mode, CRL checking, HTTP fallback";
            default:
              return Value;
          }

//        case "limittocollectionid":
//          return ReturnSubSelectValue("site", "Name", "SELECT Name FROM SMS_Query where QueryID = '" + Value + "'", Value);
        default:
          return Value;
      }
    }

    private string ServerInformation(string Field, string Value)
    {
      switch (Field.ToLower())
      {
        case "installdate":
          return ManagementDateTimeConverter.ToDateTime(Value).ToLongDateString();
        case "oslanguage":
          switch (Value)
          {
            case "1033":
              //http://msdn.microsoft.com/en-us/library/Aa394239
              return "English";
            default:
              return Value;
          }
        case "totalphysicalmemory":
          return ConvertMemory(Value);
        case "domainrole":
          switch (Value)
          {
            case "0":
              return "Standalone Workstation";
            case "1":
              return "Member Workstation";
            case "2":
              return "Standalone Server";
            case "3":
              return "Member Server";
            case "4":
            case "5":
              return "Domain Controller";
            default:
              return Value;
          }
        default:
          return Value;
      }
    }

    private string ConvertMemory(string memory)
    {
      if (memory.Trim() == "")
        return "";
      else
      {
        int liCount = 0;
        string _return = String.Empty;
        long _mem = Convert.ToInt64(memory);
        long _memtemp = _mem / 1024;
        while (Math.Abs(_memtemp) > 0)
        {
          _mem = _mem / 1024;
          _memtemp = _memtemp / 1024;
          liCount++;
        }
        switch (liCount)
        {
          case 0:
            _return = "bytes";
            break;
          case 1:
            _return = "KB";
            break;
          case 2:
            _return = "MB";
            break;
          case 3:
            _return = "GB";
            break;
          case 4:
            _return = "TB";
            break;
        }
        return Convert.ToString(_mem) + " " + _return;
      }
    }

    public string ScheduleConverter(string Value)
    {
/*
      double ch1 = (double)CharToHex(Convert.ToInt32(Value.Substring(1, 1)));
      double ch2 = (double)CharToHex(Convert.ToInt32(Value.Substring(2, 1)));
      ch1 = Math.Pow(ch1, 2);
      ch2 = Math.Pow(ch2, 2);
      int minute = (Convert.ToInt32(ch1) || Convert.ToInt32(ch2));

      ch1 = (double)CharToHex(Convert.ToInt32(Value.Substring(2, 1)));
      ch2 = (double)CharToHex(Convert.ToInt32(Value.Substring(3, 1)));
    	ch1 = ch1 % 4;
      ch1 = Math.Pow(ch1, 3);
      ch2 = Math.Pow(ch2, 1);
      bool hours = (Convert.ToBoolean(ch1) || Convert.ToBoolean(ch2));

      ch1 = (double)CharToHex(Convert.ToInt32(Value.Substring(3, 1)));
      ch2 = (double)CharToHex(Convert.ToInt32(Value.Substring(4, 1)));
      ch1 = ch1 % 2;
      ch1 = Math.Pow(ch1, 4);
      bool days = (Convert.ToBoolean(ch1) || Convert.ToBoolean(ch2));

      month = (double)CharToHex(Convert.ToInt32(Value.Substring(5, 1)));
      ch1 = (double)CharToHex(Convert.ToInt32(Value.Substring(6, 1)));
      ch2 = (double)CharToHex(Convert.ToInt32(Value.Substring(7, 1)));
      ch1 = Math.Pow(ch1, 2);
      ch2 = Math.Pow(ch1, 2);

//      double Int = 1970;

    	date= year & "/" & month & "/" & days & " " & Hours */

      return Value;
      /*



	ch1 = leftshift(ch1,2)
	Ch2 = rightshift(ch2, 2)
	Year = (ch1 or ch2 ) + 1970
	date= year & "/" & month & "/" & days & " " & Hours 
	
	if (len(minutes) = 1) then 
		date = date & ":0" & minutes
	else 
		date = date & ":" & minutes
	End if

	StrToDate = date
      */

      //Bits
      //1-5 = Duration time (Hours)
      //6-10 = Duration time (Minutes)
      /*

      Value = Value.Substring(Value.Length - 8);


      double ch1 = (double)CharToHex(Convert.ToInt32(Value.Substring(3, 1)));
      double ch2 = (double)CharToHex(Convert.ToInt32(Value.Substring(4, 1)));
      ch1 = ch1 % 4;

      ch1 = ch1 * Math.Pow(2, 1);
      ch2 = ch2 / Math.Pow(ch2, 3);

      return "0";
//      bool flag = (ch1 || ch2);


      */
    }
/*	
	Ch1 = ch1 % 4
	Ch1 = leftshift(ch1,1)
	ch2 = rightshift(ch2, 3) 


	select case flag
		Case 1 	' Non-Recuring
			s = "Bad Scheduel token"
		Case 2	' Recurinterval
			'Minutes
			ch1 = chartohex(Asc(Mid(value, 4, 1)))
			ch2 = chartohex(Asc(Mid(value, 5, 1)))
			Ch1 = leftshift(ch1,3)
			ch2 = rightshift(ch2, 1) 
			sMinutes = ch1 or ch2
		
			'Hours
			ch1 = chartohex(Asc(Mid(value, 5, 1)))
			ch2 = chartohex(Asc(Mid(value, 6, 1)))
			Ch1 = leftshift(ch1,4)
			sHours = ch1 or ch2
			

			'Days
			ch1 = chartohex(Asc(Mid(value, 7, 1)))
			ch2 = chartohex(Asc(Mid(value, 8, 1)))
			Ch1 = leftshift(ch1,1)
			ch2 = rightshift(ch2, 3) 
			sDays = ch1 or ch2


			If sMinutes <> 0 then s = "Occurs every " & sMinutes & " minutes"
			If sHours <> 0 then s = "Occurs every " & sHours & " hours"
			If sDays <> 0 then s = "Occurs every " & sDays & " days"


'			Not used
'			ch1 = chartohex(Asc(Mid(value, 8, 1)))
'			sGMT = ch1 mod 2

		Case 3	' RecurWeekly
			'Day of the week
			ch1 = chartohex(Asc(Mid(value, 4, 1)))
			sWeekDay = ch1 mod 8

			ch1 = chartohex(Asc(Mid(value, 5, 1)))
			sNumberWeeks = rightshift(ch1,1)

			s = "Occurs every " & sNumberWeeks & " week(s) on "
			select case sWeekday
				case 1
					s = s & "Sunday"
				case 2
					s = s & "Monday"
				case 3
					s = s & "Tuesday"
				case 4
					s = s & "Wednesday"
				case 5
					s = s & "Thursday"
				case 6
					s = s & "Friday"
				case 7
					s = s & "Saturday"
				case else
					s = s & "error"
			End Select
		Case 4	' RecurMonthly
			'Day of the week
			ch1 = chartohex(Asc(Mid(value, 4, 1)))
			sWeekDay = ch1 mod 8

			sNumMonths = chartohex(Asc(Mid(value, 5, 1)))

			ch1 = chartohex(Asc(Mid(value, 6, 1)))
			ch1 = rightshift(ch1,1)
			sWeekOrder = ch1 mod 8

			s = "Occurs the "
			select case sWeekOrder
				case 0
					s = s & "last "
				case 1
					s = s & "first "
				case 2
					s = s & "second "
				case 3
					s = s & "third "
				case 4
					s = s & "forth "
				case else
					s = s & "error "
			End Select
	
			select case sWeekday
				case 1
					s = s & "Sunday"
				case 2
					s = s & "Monday"
				case 3
					s = s & "Tuesday"
				case 4
					s = s & "Wednesday"
				case 5
					s = s & "Thursday"
				case 6
					s = s & "Friday"
				case 7
					s = s & "Saturday"
				case else
					s = s & "error"
			End Select
			s = s & " of every " & sNumMonths & " month(s)"
		Case 5	' RecurMonthlybyDate
			'Day of the week
			ch1 = chartohex(Asc(Mid(value, 4, 1)))
			ch2 = chartohex(Asc(Mid(value, 5, 1)))
			Ch1 = leftshift (ch1,2)
			ch2 = rightshift (ch2,2)
			sDate = ch1 or Ch2
	
			ch1 = chartohex(Asc(Mid(value, 5, 1)))
			ch2 = chartohex(Asc(Mid(value, 6, 1)))
			Ch1 = leftshift (ch1,2)
			ch2 = rightshift (ch2,2)
			sNumMonths = ch1 or ch2

			select case sdate
				case 0
					s = "Occurs the last day of every "

				case else
					s = "Occurs day " & sdate & " of every "
			end select
			s = s & sNumMonths & " month(s)"
		
		case else
			s = "Bad Schedule token"
	End Select
	Schedule = s
End Function
*/

      int CharToHex(int Value)
      {
        if ((Value >= 48) && (Value <= 57))
          return Value - 48;
        else if ((Value >= 65) && (Value <= 70))
          return Value - 55;
        else
          return 0;
      }
  }
}
